/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import beans.AuthorDAO;
import beans.AuthorHelper;
import beans.MessageBean;
import beans.SessionManager;
import beans.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "AUTHORS")
@NamedQueries({
    @NamedQuery(name = "getAuthorsForName",
    query = "SELECT a FROM Author a WHERE a.name = :authorName AND a.surname = :authorSurname"),
    @NamedQuery(name = "getAllAuthors",
    query = "SELECT a FROM Author a"),
    @NamedQuery(name = "removeAuthorForName",
    query = "DELETE FROM Author a WHERE a.name = :authorName AND a.surname = :authorSurname"),
    @NamedQuery(name = "removeAuthorForId",
    query = "DELETE FROM Author a WHERE a.id = :authorID"),
    @NamedQuery(name = "updateAuthorForId",
    query = "UPDATE Author a SET a.name= :authorName, a.surname= :authorSurname WHERE a.id = :authorID"),
    @NamedQuery(name = "updateAuthorForName",
    query = "UPDATE Author a SET a.name= :newAuthorName, a.surname= :newAuthorSurname "
            + "WHERE a.name= :formerAuthorName and a.surname= :formerAuthorSurname ")
})
public class Author implements Serializable {

    private static final long serialVersionUID = 1L;

    @ManyToMany(cascade ={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinTable(name = "AUTHORS_BOOKS", joinColumns = {
        @JoinColumn(name = "AUTHOR_ID")
    }, inverseJoinColumns = {
        @JoinColumn(name = "BOOK_ID")
    })
    private Collection<Book> books;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", nullable = false)
    private Long id;
    
    @Column(name = "NAME")
    private String name;

    @Column(name = "SURNAME")
    private String surname;

    public Author() {
        books = new ArrayList<Book>();
    }

    public Author(String name, String surname) {
        this.name = name;
        this.surname = surname;
        books = new ArrayList<Book>();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Collection<Book> getBooks() {
        return this.books;
    }

    public void setBooks(Collection<Book> books) {
        this.books = books;
    }
    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Author)) {
            return false;
        }
        Author other = (Author) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Author[id=" + id + "]";
    }
    public String getFullName()
    {
        return name + " " + surname;
    }
    public int getBooksAmount()
    {
        return books.size();
    }
    public void delete() throws NonexistentEntityException {
        //SessionManager sm = new SessionManager();
        //sm.clearSessionItem("book"); //sprawdzic czy potrzebne

        SessionManager.getInstance().setRequestItem("messageBean", new MessageBean("Usunięto autora "+name+" "+surname));
        new AuthorDAO().destroy(id);
    }

    public String edit()
    {
        //SessionManager sm = new SessionManager();
        //sm.setRequestItem("book", this);
        SessionManager.getInstance().setRequestItem("authorHelper", new AuthorHelper(this));
        return "edit";
    }

    public void update() throws NonexistentEntityException, Exception {
        new AuthorDAO().edit(this);
    }
}
